package com.itheima.controller;

import com.itheima.pojo.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class CookieController {
    /**
     * 设置cookie
     * @param resp
     * @return
     */
    @GetMapping("/setCookie")
    public Result setCookie (HttpServletResponse resp){
        Cookie cookie = new Cookie("kunkun","jinitaimei");//初始化cookie的k-v属性名称
        cookie.setMaxAge(6666);//设置cookie存活时间
        resp.addCookie(cookie);
        return Result.success();
    }

    @GetMapping("/getCookie")
    public Result getCookie(HttpServletRequest req){
        Cookie[] cookies = req.getCookies();
        if (cookies!=null&&cookies.length>0){
            for (Cookie cookie : cookies) {
                log.info("cookie name:{},value:{}",cookie.getName(),cookie.getValue());
            }
        }
        return Result.success();
    }
}
